<?php

declare(strict_types=1);

namespace App\Model\Setting;

use Hyperf\DbConnection\Model\Model;

/**
 *  配置 - 用户标签
 */
class SettingUserTags extends Model
{
    protected $table = 'setting_user_tags';

    // const TYPE_LOOK = 1;
    // const TYPE_HOBBY = 2;
    const TYPE_ID = 3;
    const TYPE_LOVE = 4;
    const TYPE_FRD = 5;
    const TYPE_FIGURE = 6;
    const TYPE_DRESS = 7;
    const TYPE_PERSON = 8;
    const TYPE_GOAL = 9;
    const TYPE_LOVE_TAG = 10;
    const TYPE = [
        // self::TYPE_LOOK => '外形',
        // self::TYPE_HOBBY => '兴趣爱好',
        self::TYPE_ID => '身份',
        self::TYPE_LOVE => '感情状况',
        self::TYPE_FRD => '交友范围',
        self::TYPE_FIGURE => '身材特性',
        self::TYPE_DRESS => '日常装扮',
        self::TYPE_PERSON => '性格',
        self::TYPE_GOAL => '使用软件目的',
        self::TYPE_LOVE_TAG => '喜欢的类型',
    ];

    protected $fillable = ['tag_name', 'tag_type'];

    static function addRecord($tag = '', $tagType = self::TYPE_ID)
    {
        return self::create([
            'tag_name' => $tag,
            'tag_type' => $tagType,
        ]);
    }

    static function getList($gender = -1, $tagType = 0)
    {
        $where = [['flag', 1]];
        //标签类型
        if ($tagType) $where[] = ['tag_type', $tagType];
        else $where[] = ['tag_type', '<>', self::TYPE_LOVE_TAG];
        $data = self::where($where)->whereIn('tag_gender', [-1, $gender])->get()->toArray();
        $ret = [];
        foreach ($data as $item) {
            if (!isset($ret[$item['tag_type']])) {
                $ret[$item['tag_type']]['type_name'] = self::TYPE[$item['tag_type']] ?? '未知';
                $ret[$item['tag_type']]['tags'] = [];
            }
            $ret[$item['tag_type']]['tags'][] = [
                'tag_id' => $item['id'],
                'tag_name' => $item['tag_name'],
            ];
        }
        return array_values($ret);
    }

    /**
     * 标签数量
     */
    static function getTotalNum($tagType = 0, $gender = -1)
    {
        $where = [
            ['flag', 1]
        ];
        if ($tagType) $where[] = ['tag_type', $tagType];
        else $where[] = ['tag_type', '<>', self::TYPE_LOVE_TAG];
        // $gender >= 0 && $where[] = ['tag_gender', $gender];
        return self::where($where)->whereIn('tag_gender', [-1, $gender])->count();
    }
}
